package cn.wolfcdoe.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.wolfcdoe.business.domain.vo.AuditInfoVo;
import cn.wolfcdoe.common.constant.BusServiceItemConstants;
import cn.wolfcdoe.common.core.domain.entity.SysUser;
import cn.wolfcdoe.common.utils.ActivitiesUtils;
import cn.wolfcdoe.common.utils.DateUtils;
import cn.wolfcdoe.system.service.impl.SysUserServiceImpl;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcdoe.business.mapper.BusServiceItemMapper;
import cn.wolfcdoe.business.domain.BusServiceItem;
import cn.wolfcdoe.business.service.IBusServiceItemService;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 服务项Service业务层处理
 * 
 * @author zhuo
 * @date 2023-04-08
 */
@Service
public class BusServiceItemServiceImpl implements IBusServiceItemService 
{
    @Resource
    private BusServiceItemMapper busServiceItemMapper;

    @Resource
    private SysUserServiceImpl sysUserService;

    @Resource
    private RepositoryService repositoryService;                    //Activiti的资源管理接口


    /**
     * 查询服务项
     * 
     * @param id 服务项主键
     * @return 服务项
     */
    @Override
    public BusServiceItem selectBusServiceItemById(Long id)
    {
        return busServiceItemMapper.selectBusServiceItemById(id);
    }

    /**
     * 查询服务项列表
     * 
     * @param busServiceItem 服务项
     * @return 服务项
     */
    @Override
    public List<BusServiceItem> selectBusServiceItemList(BusServiceItem busServiceItem)
    {
        return busServiceItemMapper.selectBusServiceItemList(busServiceItem);
    }

    /**
     * 新增服务项
     * 
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int insertBusServiceItem(BusServiceItem busServiceItem)
    {
        busServiceItem.setCreateTime(DateUtils.getNowDate());

        //业务判断
        Assert.notNull(busServiceItem,"数据不能为空");
        String name = busServiceItem.getName();                             //服务名称
        BigDecimal originalPrice = busServiceItem.getOriginalPrice();       //服务原价
        BigDecimal discountPrice = busServiceItem.getDiscountPrice();       //服务折扣价

        Assert.state(originalPrice.compareTo(discountPrice)>=0,"服务项折扣价不能大于原价");
        Integer carPackage = busServiceItem.getCarPackage();                //套餐信息
        int audit = carPackage == BusServiceItemConstants.CAR_PACKAGE_YES       //判断是否是套餐
                ? BusServiceItemConstants.AUDIT_STATUS_INIT         //初始化
                : BusServiceItemConstants.AUDIT_STATUS_NOT_NEED;     //无需审核
        //设置审核状态
        busServiceItem.setAuditStatus(audit);
        Integer serviceCatalog = busServiceItem.getServiceCatalog();        //服务分类
        Assert.notNull(serviceCatalog,"请选择服务分类");
        String info = busServiceItem.getInfo();                             //备注信息
        //设置上架状态都是未上架
        busServiceItem.setSaleStatus(BusServiceItemConstants.SALE_STATUS_OFF);


        return busServiceItemMapper.insertBusServiceItem(busServiceItem);
    }

    /**
     * 修改服务项
     * 
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int updateBusServiceItem(BusServiceItem busServiceItem)
    {
        //业务判断
        Assert.notNull(busServiceItem,"数据不能为空");
        Long id = busServiceItem.getId();
        Assert.notNull(id,"非法参数");

        BusServiceItem busServiceItem1 = busServiceItemMapper.selectBusServiceItemById(id);
        Assert.notNull(busServiceItem1,"修改的服务项不存在");
        int auditStatus = busServiceItem1.getAuditStatus();

        Assert.state(auditStatus == BusServiceItemConstants.AUDIT_STATUS_INIT
                || auditStatus== BusServiceItemConstants.AUDIT_STATUS_NOT_NEED,"服务项审核状态不支持");

        //已上架的服务项不支持修改
        int saleStatus = busServiceItem1.getSaleStatus();
        Assert.state(saleStatus == BusServiceItemConstants.SALE_STATUS_OFF,"已上架的服务项不支持修改");

        BigDecimal originalPrice = busServiceItem.getOriginalPrice();       //服务原价
        BigDecimal discountPrice = busServiceItem.getDiscountPrice();       //服务折扣价

        Assert.state(originalPrice.compareTo(discountPrice)>=0,"服务项折扣价不能大于原价");
        Integer carPackage = busServiceItem.getCarPackage();                //套餐信息
        int audit = carPackage == BusServiceItemConstants.CAR_PACKAGE_YES       //判断是否是套餐
                ? BusServiceItemConstants.AUDIT_STATUS_INIT         //初始化
                : BusServiceItemConstants.AUDIT_STATUS_NOT_NEED;     //无需审核
        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    /**
     * 批量删除服务项
     * 
     * @param ids 需要删除的服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemByIds(Long[] ids)
    {
        return busServiceItemMapper.deleteBusServiceItemByIds(ids);
    }

    /**
     * 删除服务项信息
     * 
     * @param id 服务项主键
     * @return 结果
     */
    @Override
    public int
    deleteBusServiceItemById(Long id)
    {
        return busServiceItemMapper.deleteBusServiceItemById(id);
    }

    /**
     * 修改上下架状态信息
     * @param id 服务项主键
     * @return 结果
     */
    @Override
    public int saveBusServiceItemById(Long id) {
        Assert.notNull(id,"非法参数");
        BusServiceItem busServiceItem1 = busServiceItemMapper.selectBusServiceItemById(id);
        Assert.notNull(busServiceItem1,"修改的服务项不存在");
        int auditStatus = busServiceItem1.getAuditStatus();
        int saleStatus = busServiceItem1.getSaleStatus();
        String msg = saleStatus == BusServiceItemConstants.SALE_STATUS_ON ? "下架": "上架";
        Assert.state(auditStatus == BusServiceItemConstants.AUDIT_STATUS_SUCCESS
                || auditStatus== BusServiceItemConstants.AUDIT_STATUS_NOT_NEED,"服务项审核未通过不支持"+msg);

        saleStatus = saleStatus == BusServiceItemConstants.SALE_STATUS_ON
                ? BusServiceItemConstants.SALE_STATUS_OFF
                : BusServiceItemConstants.SALE_STATUS_ON;
        busServiceItem1.setSaleStatus(saleStatus);
        return busServiceItemMapper.updateBusServiceItem(busServiceItem1);
    }

    @Override
    public AuditInfoVo serviceItemAuditInfo(Long id) {

        //查询出服务项的数据
        AuditInfoVo auditInfoVo = new AuditInfoVo();
        BusServiceItem busServiceItem = busServiceItemMapper.selectBusServiceItemById(id);
        auditInfoVo.setBusServiceItem(busServiceItem);
        //查询出店长的数据
        List<SysUser> shopOwners = sysUserService.selectUserByRoleName("店长");
        //设置到vo对象中返回给到前端
        auditInfoVo.setShopOwners(shopOwners);

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("bus_car_package")
                .singleResult();
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());

        BigDecimal discountPrice = ActivitiesUtils.getGTEDiscountPrice(bpmnModel, "bus_car_package");
        //设置阈值
        auditInfoVo.setDiscountPrice(discountPrice);
        if (busServiceItem.getDiscountPrice().compareTo(discountPrice) >= 0) {
            //查询出财务的数据
            List<SysUser> finances = sysUserService.selectUserByRoleName("财务");
            auditInfoVo.setFinances(finances);
        }
        return auditInfoVo;
    }
}
